#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
# @Time    : 2024/11/26 11:47
# @Author  : zmz
# @File    : Investment_payback_period.py
# @function: 投资回收期
# @Software: PyCharm

import PySimpleGUI as sg
import numpy_financial as npf

import debug_infomation as _debug
import public_data as _pub

# 动态投资回收期布局
dynamics_layout = [
    [sg.Text('输入以下投资信息：')],
    [sg.Text('1.初始投资金额', size=(12, 1)), sg.InputText(key='-PV-', default_text="-12000")],
    [sg.Text('(输入负数形式，如 -1000)')],
    [sg.Text('2.每期回收金额', size=(12, 1)), sg.InputText(key='-PMT-', default_text="3000")],
    [sg.Text('3.利率 ', size=(12, 1)), sg.InputText(key='-RATE-', default_text="0.15", readonly='true')],
    [sg.Text('(小数形式)')],
    [sg.Text('4.预期未来值 ：', size=(12, 1)), sg.InputText(key='-FV-', default_text='0')],
    [sg.Text('(默认为0，可不填)')],
    [sg.Button('计算动态投资回收期')]
]

# 差额投资回收期窗口布局
diff_layout = [
    [sg.Text("方案一 期初投资："), sg.InputText(key="investment_1", default_text="1000")],
    [sg.Text("方案一 经营成本："), sg.InputText(key="operating_cost_1", default_text="500")],
    [sg.Text("方案二 期初投资："), sg.InputText(key="investment_2", default_text="2000")],
    [sg.Text("方案二 经营成本："), sg.InputText(key="operating_cost_2", default_text="400")],
    [sg.Button("计算差额投资回收期")],
    [sg.Text("", key="result")]
]


# 标准接口实现. 每个计算都需要包含.
def update_pub_data(pub_data):
    if not pub_data:
        dynamics_layout[4][1].update(_pub.pub_r)
        return
    dynamics_layout[4][1].update(pub_data['PUB_I_INPUT-'])


# 定义计算差额投资回收期的函数
def calculate_difference_payback_period(initial_investment_1, operating_cost_1, initial_investment_2,
                                        operating_cost_2):
    if initial_investment_1 > initial_investment_2:
        difference_investment = initial_investment_1 - initial_investment_2
        difference_operating_cost = operating_cost_2 - operating_cost_1
    else:
        difference_investment = initial_investment_2 - initial_investment_1
        difference_operating_cost = operating_cost_1 - operating_cost_2

    payback_period = difference_investment / difference_operating_cost

    if payback_period <= 0:
        return "结果非正，无法计算差额投资回收期。"
    else:
        return payback_period


# 动态投资回收期处理
def dynamics_Investment_handle(values):
    # 获取输入的现金流数据并转换为列表
    try:
        pv = float(values['-PV-'].replace(',', ''))
        pmt = float(values['-PMT-'].replace(',', ''))
        rate = float(values['-RATE-'].replace(',', ''))
        fv = float(values['-FV-'].replace(',', ''))
        # 使用NPER函数计算投资回收期
        nper_value = npf.nper(rate, pmt, pv, fv)
        temp_value = "{:.3f}".format(nper_value)
        _debug.info('投资回收期为（期）：{}'.format(temp_value))
    except ValueError:
        sg.popup('请确保输入的都是有效的数字！')


# 差额投资回收期处理
def diff_Investment_handle(window, values):
    try:
        investment_1 = float(values["investment_1"].replace(',', ''))
        operating_cost_1 = float(values["operating_cost_1"].replace(',', ''))
        investment_2 = float(values["investment_2"].replace(',', ''))
        operating_cost_2 = float(values["operating_cost_2"].replace(',', ''))

        result = calculate_difference_payback_period(investment_1, operating_cost_1, investment_2, operating_cost_2)
        _debug.info('差额投资回收期为(期)：{}'.format(result))
    except ValueError:
        window["result"].update("请输入正确的数字！")
